<?php

namespace console\controllers;

use Yii;
use services\statistics\ProjectDoctorService;
use yii\console\Controller;

class ReportProjectController extends Controller
{
    /**
     * php yii report-project/create-report 2020-01-01 2020-01-31
     * 每日执行时传0
     *
     * 每天生成（项目、医生）报表
     */
    public function actionCreateReport($start, $end)
    {
        try {
            $startTime = isset($start) && $start ? strtotime($start) : strtotime(date('Y-m-d', time() - 86400));
            $endTime = isset($end) && $end ? (strtotime($end) + 86400) : (strtotime(date('Y-m-d', time())));
            $days = ($endTime - $startTime) / 86400;
            if ($days > 0) {
                for ($i = 0; $i < $days; $i++) {
                    ProjectDoctorService::createProjectReport(($startTime + $i * 86400), ($startTime + $i*86400 + 86399));
                    ProjectDoctorService::createDoctorReport(($startTime + $i * 86400), ($startTime + $i*86400 + 86399));
                    $start = date('Y-m-d', $startTime + $i * 86400);
                    echo "开始时间：{$start}".PHP_EOL;
                }
            } else {
                echo '开始时间不能大于结束时间';
            }
        } catch (\Exception $e) {
            echo $e->getMessage().PHP_EOL;
        }
    }

    /**
     * 新增旬项目数据
     * 每月1/11/21执行
     *
     * @return bool|int
     */
    public function actionCreateReportXun($start, $end)
    {
        try {
            if (date('d') == "01") { // 每月一号查上个月全月
                $startTime = isset($start) && $start ? strtotime($start) : strtotime(date('Y-m', strtotime((date('Y-m'))) - 1));
                $endTime = isset($end) && $end ? (strtotime($end) + 86399) : (strtotime(date('Y-m')) - 1);
            } else {
                $startTime = isset($start) && $start ? strtotime($start) : strtotime(date('Y-m'.'-01'));
                $endTime = isset($end) && $end ? (strtotime($end) + 86399) : (strtotime(date('Y-m-d')) - 1);
            }
            $days = $endTime - $startTime;
            if ($days > 0) {
                ProjectDoctorService::createProjectReport($startTime, $endTime);
                ProjectDoctorService::createDoctorReport($startTime, $endTime);
                echo "项目结束时间：{$end}".PHP_EOL;
            } else {
                echo '开始时间不能大于结束时间';
            }
        } catch (\Exception $e) {
            echo $e->getMessage().PHP_EOL;
        }
    }
}
